package com.whcoding.rabbitmq.example.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    private static final Logger logger = LoggerFactory.getLogger(RabbitConfig.class);

    /**
     * 订单发送 ROUTING_KEY
     */
    public final static String ORDER_CODE_SEND_ROUTING_KEY = "order.send.routingkey";

    public final static String ORDER_CODE_SEND_QUEUE = "order.send.queue";


    /**
     * 订单签收 ROUTING_KEY
     */
    public final static String ORDER_CODE_SIGN_ROUTING_KEY = "order.sign.routingkey";

    public final static String ORDER_CODE_SIGN_QUEUE = "order.sign.queue";


    /**
     * 订单中心交换机
     */
    public final static String TOPIC_EXCHANGE_CENTRE = "order.exchange.centre";


    @Bean(name = "orderSendQueue")
    public Queue creatOrderSendQueue() {
        logger.info("创建订单发送消费队列");
        return new Queue(ORDER_CODE_SEND_QUEUE, true);
    }

    @Bean(name = "orderSignQueue")
    public Queue creatOrderSignQueue() {
        logger.info("创建订单签收消费队列");
        return new Queue(ORDER_CODE_SIGN_QUEUE, true);
    }


    /**
     * 创建中心交换机
     *
     * @return
     */
    @Bean(name = "orderTopicExchange")
    public TopicExchange taskTopicExchange() {
        TopicExchange topicExchange = new TopicExchange(TOPIC_EXCHANGE_CENTRE);
        return topicExchange;
    }


    /**
     * 中交兴路 绑定队列 与Exchange
     *
     * @param taskTopicExchange
     * @param eportTaskQueue
     * @return
     */
    @Bean
    public Binding bindingSendQueueCentreExchange(@Qualifier("orderTopicExchange") TopicExchange taskTopicExchange,
                                                  @Qualifier("orderSendQueue") Queue eportTaskQueue) {
        //绑定exchange 和队列
        Binding binding = BindingBuilder.bind(eportTaskQueue).to(taskTopicExchange).with(ORDER_CODE_SEND_ROUTING_KEY);
        return binding;
    }



    @Bean
    public Binding bindingSignQueueCentreExchange(@Qualifier("orderTopicExchange") TopicExchange taskTopicExchange,
                                                  @Qualifier("orderSignQueue") Queue eportTaskQueue) {
        //绑定exchange 和队列
        Binding binding = BindingBuilder.bind(eportTaskQueue).to(taskTopicExchange).with(ORDER_CODE_SIGN_ROUTING_KEY);
        return binding;
    }


}
